package com.munhrasoft.elearning.business.logic;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class GenericEJB<T> {

    @PersistenceContext(unitName = "elearningPU")
    private EntityManager em;

    public EntityManager getEm() {
        return em;
    }

    public void setEm(EntityManager em) {
        this.em = em;
    }

    public T createEntity(T entity) {
        em.persist(entity);
        return entity;
    }

    public T updateEntity(T entity) {
        return em.merge(entity);
    }

    public void removeEntity(T entity) {
        em.remove(em.merge(entity));
    }
    
    public T findEntity(T entity, Long id){
        return (T) em.find(entity.getClass(), id);
    }

}
